/**
 * 
 */
package com.gr.staffpm.pages.schedule;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;

import com.gr.staffpm.gof.observer.Subscribable;
import com.gr.staffpm.gof.observer.Subscriber;
import com.gr.staffpm.model.StringModel;
import com.gr.staffpm.pages.behaviour.ExtCalendarToolbarBehaviour;
import com.gr.staffpm.pages.schedule.model.View;

/**
 * @author Graham Rhodes 19 Mar 2011 12:11:10
 */
public class ExtCalendarToolbar extends Panel implements Subscribable {

    private static final long serialVersionUID = 1L;
    private final AjaxButton prevPage;
    private final AjaxButton dayView, weekView, monthView;
    private final AjaxButton nextPage;
    private final WebMarkupContainer dayViewContainer = new WebMarkupContainer("dayViewContainer"), weekViewContainer = new WebMarkupContainer("weekViewContainer"),
            monthViewContainer = new WebMarkupContainer("monthViewContainer");
    private final Form<Void> toolbarForm = new Form<Void>("toolbarForm");
    private final List<Subscriber> subscribers = new ArrayList<Subscriber>();
    private View currentView = View.Month;

    public ExtCalendarToolbar(String id) {
        super(id);

        prevPage = new AjaxButton("prevPage") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                prevPageClicked(target);
            }
        };
        toolbarForm.add(prevPage);

        dayView = new AjaxButton("dayView", new StringModel("Day")) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                dayViewClicked(target);
            }
        };
        dayViewContainer.add(dayView);

        weekView = new AjaxButton("weekView", new StringModel("Week")) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                weekViewClicked(target);
            }
        };
        weekViewContainer.add(weekView);

        monthView = new AjaxButton("monthView", new StringModel("Month")) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                monthViewClicked(target);
            }
        };
        monthViewContainer.add(monthView);

        nextPage = new AjaxButton("nextPage") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                nextPageClicked(target);
            }
        };
        toolbarForm.add(nextPage);

        toolbarForm.add(dayViewContainer);
        toolbarForm.add(weekViewContainer);
        toolbarForm.add(monthViewContainer);
        toolbarForm.add(new ExtCalendarToolbarBehaviour(this));

        add(toolbarForm);

    }

    protected void monthViewClicked(AjaxRequestTarget target) {
        currentView = View.Month;
        notifySubscribers(Schedule.TBAR_MONTH_VIEW, target);
    }

    protected void weekViewClicked(AjaxRequestTarget target) {
        currentView = View.Week;
        notifySubscribers(Schedule.TBAR_WEEK_VIEW, target);
    }

    protected void dayViewClicked(AjaxRequestTarget target) {
        currentView = View.Day;
        notifySubscribers(Schedule.TBAR_DAY_VIEW, target);
    }

    protected void nextPageClicked(AjaxRequestTarget target) {
        notifySubscribers(Schedule.TBAR_NEXT_PAGE, target);
    }

    protected void prevPageClicked(AjaxRequestTarget target) {
        notifySubscribers(Schedule.TBAR_PREV_PAGE, target);
    }

    public View getCurrentView() {
        return currentView;
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.gof.observer.Subscribable#addSubscriber(com.gr.staffpm.gof.observer.Subscriber)
     */
    @Override
    public void addSubscriber(Subscriber subscriber) {
        subscribers.add(subscriber);
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.gof.observer.Subscribable#notifySubscribers(java.lang.Object, org.apache.wicket.ajax.AjaxRequestTarget)
     */
    @Override
    public void notifySubscribers(Object object, AjaxRequestTarget target) {
        for (Subscriber subscriber : subscribers)
            subscriber.notifySubscriber(object, target);
    }

}
